package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.rpframework.core.BaseAct;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Category;
import com.rpframework.website.device.service.CategoryService;

/**
 * 商品类别模块接口
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api/category")
public class CategoryApiAct extends BaseAct {
	@Resource CategoryService service;

	
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String listone(@RequestParam(value="pager",required = false ) Pager<Category> pager,
			
			@RequestParam(value="name",required = false ) String name,/*状态 1.正常 2.禁用*/
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<Category>();
		}
		
		if(StringUtils.isNotBlank(name))pager.getSearchMap().put("name", name);
		pager = service.getPager(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<Category> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(Category t : list){
				JsonObject obj = new JsonObject();
				obj.addProperty("id", t.getId());	
				JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
				
				JsonArray a = new JsonArray();
				for(JsonElement li : arr){
					a.add(new JsonPrimitive(TagUtils.getFileFullPath(li.toString().replace("\"", ""))));
				}
				obj.add("urlList", a);


				
				
			//	obj.add("urlList", arr);//类别商品图片
				obj.addProperty("name", t.getName());//类别名字
				
				array.add(obj);
			}
			json.add("arrays", array);
		}
	if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
	return   ""+json  ;
	}	
	
}
